/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Locatie;
import model.Masterclass;

/**
 *
 * @author daveronvanderham
 */
public class MasterclassToevoegen extends javax.swing.JFrame {

    /**
     * Creates new form MasterclassToevoegen
     */
    public MasterclassToevoegen() {
        initComponents();
        if (!Database.openConnection("fullhouse", "root", "")) {

            System.out.println("Geen verbinding met de database");
            System.exit(0);
        }
        vulcombo();
        vulTabelLocatie();
    }

private void vulTabelLocatie() {
        //Ophalen van de toernooien uit de database

        Locatie locatie = new Locatie();
        String[] kolommen = {"Naam", "Plaats", "Straat", "Huisnummer", "Postcode"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        int aantalLocaties = locatie.haalAantalLocatiepOp();

        //Lijstje aanmaken voor de tennistoernooien die we uit de database gaan halen. 
        Locatie[] locaties;
        if (aantalLocaties == -1) {
            //return null;
        } else {
            locaties = new Locatie[aantalLocaties];
        }

        try {

            String sql = "SELECT * FROM locatie";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            int teller = 0;
            while (resultaat.next()) {

                String huisnummer = resultaat.getString("locatie_huisnummer");
                String postcode = resultaat.getString("locatie_postcode");

                Locatie tabelVulling = new Locatie(huisnummer, postcode);
                model.addRow(tabelVulling.getInformatie());



            }


        } catch (SQLException e) {
        }

        locatieTable.setModel(model);

    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        masterclassdatum = new javax.swing.JTextField();
        masterclassrating = new javax.swing.JTextField();
        masterclassdeelnemers = new javax.swing.JTextField();
        masterBox = new javax.swing.JComboBox();
        masterclassinleggeld = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        locatieTable = new javax.swing.JTable();
        masterclassregistreer = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        masterclasstijd = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        postcode = new javax.swing.JLabel();
        huisnr = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("masterclass toevoegen");

        masterclassdatum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masterclassdatumActionPerformed(evt);
            }
        });

        jLabel2.setText("master");

        jLabel3.setText("datum");

        jLabel4.setText("minimale rating");

        jLabel5.setText("minnimale deelnemers");

        jLabel6.setText("inleggeld");

        locatieTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "naam", "plaats", "straat", "huisnummer", "postcode", "max_ deelnemers"
            }
        ));
        locatieTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                locatieTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(locatieTable);
        locatieTable.getColumnModel().getColumn(2).setResizable(false);
        locatieTable.getColumnModel().getColumn(4).setResizable(false);

        masterclassregistreer.setText("registreer");
        masterclassregistreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masterclassregistreerActionPerformed(evt);
            }
        });

        jLabel7.setText("locatie");

        jLabel8.setText("tijd");

        postcode.setText("postcode");

        huisnr.setText("huisnr");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(0, 0, Short.MAX_VALUE)
                .add(jLabel7)
                .add(235, 235, 235))
            .add(layout.createSequentialGroup()
                .add(17, 17, 17)
                .add(jLabel1)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .add(31, 31, 31)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(masterclassregistreer)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel5)
                            .add(jLabel6)
                            .add(jLabel8))
                        .add(75, 75, 75)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(huisnr)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(masterBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(masterclassrating, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                                .add(masterclassdeelnemers)
                                .add(masterclassinleggeld)
                                .add(masterclassdatum)
                                .add(masterclasstijd))
                            .add(postcode))))
                .add(16, 16, 16)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                .add(12, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(19, 19, 19)
                        .add(jLabel1))
                    .add(jLabel7))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterclasstijd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterclassdatum, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterclassrating, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterclassdeelnemers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(masterclassinleggeld, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel6))
                        .add(18, 18, 18)
                        .add(postcode)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 29, Short.MAX_VALUE)
                        .add(huisnr)
                        .add(18, 18, 18)
                        .add(masterclassregistreer)
                        .add(24, 24, 24))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void masterclassregistreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masterclassregistreerActionPerformed
        // TODO add your handling code here:

        if (!masterclassdatum.getText().matches("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) {
            JOptionPane.showMessageDialog(null, "datum moet yyyy-mm-dd zijn.");
        } else if (!masterclasstijd.getText().matches("^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$")) {
            JOptionPane.showMessageDialog(null, "tijd moet 00:00:00 zijn.");
        } else if (!masterclassrating.getText().matches("^\\d+$")) {
            JOptionPane.showMessageDialog(null, "rating mag alleen cijfers bevatten");
        } else if (!masterclassdeelnemers.getText().matches("^\\d+$")) {
            JOptionPane.showMessageDialog(null, "deelnemers mag alleen cijfers bevatten");
        } else if (!masterclassinleggeld.getText().matches("^\\d+$")) {
            JOptionPane.showMessageDialog(null, "bedrag mag alleen cijfers bevatten");
        } else if (!postcode.getText().matches("[0-9]{4}[A-Z]{2}")) {
            JOptionPane.showMessageDialog(null, "postcode klopt niet");
        } else if (!huisnr.getText().matches("^([0-9]{1,}+([A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "huisnummer klopt niet");
        } else {

            try {
                String datum = masterclassdatum.getText();
                String tijd = masterclasstijd.getText();
                String rating = masterclassrating.getText();
                int deelnemers = Integer.parseInt(masterclassdeelnemers.getText());
                double inleggeld = Double.parseDouble(masterclassinleggeld.getText());
                String Postcode = (postcode.getText());
                String Huisnr = (huisnr.getText());
                int Master = (masterBox.getSelectedIndex());
                String status = "actief";

                Masterclass registreren = new Masterclass(Master, datum, tijd, rating, deelnemers, inleggeld, Postcode, Huisnr , status);
                registreren.setMaster(Master);

                if (registreren.insert()) {
                    this.dispose();
                } else {
                }
                System.out.println("masterclass toevoegen: opslaan: " + "error bij opslaan");
            } catch (Exception e) {
            }
        }


    }//GEN-LAST:event_masterclassregistreerActionPerformed

    private void masterclassdatumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masterclassdatumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_masterclassdatumActionPerformed

    private void locatieTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_locatieTableMouseClicked
        // TODO add your handling code here:
        int row = locatieTable.getSelectedRow();
        huisnr.setText((String) locatieTable.getModel().getValueAt(row, 3));
        postcode.setText((String) locatieTable.getModel().getValueAt(row, 4));

    }//GEN-LAST:event_locatieTableMouseClicked

    private void vulcombo() {

        try {
            String query = "SELECT * FROM master";
            ResultSet rs = Database.executeSelectQuery(query);

            while (rs.next()) {

                String naam = rs.getString("master_bijnaam");
                masterBox.addItem(naam);


            }
        } catch (Exception e) {
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MasterclassToevoegen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel huisnr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable locatieTable;
    private javax.swing.JComboBox masterBox;
    private javax.swing.JTextField masterclassdatum;
    private javax.swing.JTextField masterclassdeelnemers;
    private javax.swing.JTextField masterclassinleggeld;
    private javax.swing.JTextField masterclassrating;
    private javax.swing.JButton masterclassregistreer;
    private javax.swing.JTextField masterclasstijd;
    private javax.swing.JLabel postcode;
    // End of variables declaration//GEN-END:variables
}
